<template>
  <div>
    <el-button-group id="btn">
      <el-button @click="selectHistoryByStatus(0)" type="primary">仅浏览</el-button>
      <el-button @click="selectHistoryByStatus(1)" type="success">成功记录</el-button>
      <el-button @click="selectHistoryByStatus(2)" type="warning">错题本</el-button>
    </el-button-group>
    <el-table
        :data="tableData"
        style="width: 100%"
        @row-click="goToDetail">
      <el-table-column
          prop="createTime"
          label="日期">
      </el-table-column>
      <el-table-column
          prop="rangeName"
          label="实验名称">
      </el-table-column>
    </el-table>

    <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[10, 20, 30]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        class="pagination">
    </el-pagination>
  </div>
</template>

<script>
import Cookies from "js-cookie";
import axios from "axios";

export default {
  name: "History",
  data() {
    return {
      currentPage: 1,
      pageSize: 10,
      total: 0,
      tableData: [],
      status: 0
    }
  },
  methods: {
    initHistory() {
      let userToken = Cookies.get('user');
      console.log("userToken/////////////")
      console.log(userToken)

      let params = {
        token: userToken,
        currentPage: this.currentPage,
        pageSize: this.pageSize,
        status: this.status
      }
      this.$axios.post("/history/selectHistoryByUserId", params).then((res) => {
        this.tableData = res.data.records;
        this.total = res.data.total;
      })
    },
    selectHistoryByStatus(value) {
      this.status = value;
      this.currentPage = 1;
      this.initHistory();
    },
    handleSizeChange(size) {
      this.currentPage = 1;
      this.pageSize = size;
      this.initHistory();
    },
    handleCurrentChange(currentPage) {
      this.currentPage = currentPage;
      this.initHistory();
    },
    goToDetail(row) {
      this.$router.push({
        name: 'Detail',
        params: {id: row.rangeId},
      });
    }
  },
  created() {
    this.initHistory();
  }
}
</script>

<style scoped>
.pagination {
  text-align: center;
}
</style>